/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import explorer.ExplorerUnited;
import explorer.FileUnited;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author user1
 */
@WebServlet(name = "servletUNITED", urlPatterns = {"/servletUNITED"})
public class ServletUnited extends HttpServlet {

    public static final String DOCTYPE =
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet servletUNITED</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet servletUNITED at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }
    
    public static String headWithTitle(String title) {
    return(DOCTYPE + "\n" +
           "<HTML>\n" +
           "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n");
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
     public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Reading All Request Parameters";
    out.println(headWithTitle(title) +
                "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
                "<TABLE BORDER=1 ALIGN=CENTER>\n" +
                "<TR BGCOLOR=\"#FFAD00\">\n" +
                "<TH>File Name<TH>File Extension");
    //Enumeration paramNames = request.getParameterNames();
    ExplorerUnited.setDir(request.getParameterValues("dir"));
    ExplorerUnited.setExplore(Integer.parseInt((request.getParameterValues("explore"))[0]));
    ExplorerUnited.setSearch(Integer.parseInt((request.getParameterValues("metode"))[0]));
    ExplorerUnited.startExplore(request.getParameterValues("query"));
    for (FileUnited fileUnited : ExplorerUnited.result) {
        out.println("<TR><TD>" + "<a href=\"" +
                fileUnited.getFullPath() + "\">" +
                fileUnited.getName() + "</a>\n<TD>" + 
                fileUnited.getExtension() + "</TR>");
        if (fileUnited.startChar.size() > 0) {
            Collections.sort(fileUnited.startChar);
            int prev = fileUnited.startChar.get(0);
            for (int i = 1; i < fileUnited.startChar.size(); i++) {
                if (fileUnited.startChar.get(i) - prev <= 50) {
                    fileUnited.startChar.remove(i);
                    i--;
                }
            }
            for (int i = 0; i < fileUnited.startChar.size(); i++) {
                out.println("<TR><TD colspan=2>" + "..." +
                        fileUnited.getContent().substring(((fileUnited.startChar.get(i) - 25) >= 0 ? (fileUnited.startChar.get(i) - 25) : 0), fileUnited.startChar.get(i) +60) 
                        + "..." + "</TD></TR>");
            }
        } else {
            out.println("<TR><TD colspan=2>"+ "...................." + "</TD></TR>");
        }
    }
    
    out.println("</TABLE>\n</BODY></HTML>");
  }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
      protected void doPost(HttpServletRequest request,
                         HttpServletResponse response)
          throws ServletException, IOException {
        doGet(request, response);
      }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
